package conpool;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.bouncycastle.jcajce.provider.symmetric.Threefish;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Pool {
	
//    private static DataSource dataSource0 = null;

	private  String Driver;
	private  String table;
	private  String name;
	private  String password;
	private  String dataSourceName;

    private ComboPooledDataSource dataSource = new ComboPooledDataSource();
	
	public Pool(String Driver,String table,String name,String password,String dataSourceName) {
		this.Driver = Driver;
		this.table = table;
		this.name = name;
		this.password = password;
		this.dataSourceName = dataSourceName;

	     try {
				dataSource.setDriverClass(Driver);
			} catch (PropertyVetoException e) {
				e.printStackTrace();
			}
	     
	     dataSource.setJdbcUrl(table);
	     dataSource.setUser(name);
	     dataSource.setPassword(password);
	     dataSource.setDataSourceName(dataSourceName);
	     dataSource.setMaxPoolSize(50);
	     dataSource.setMinPoolSize(1);
	}
    
    public Pool() {
	     
//	     System.out.println(dataSource.getMaxPoolSize());// 最大连接数
//	     System.out.println(dataSource.getMinPoolSize());// 最小连接数
//	     try {
////			System.out.println(dataSource.getNumBusyConnections());
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}// 正在使用连接数
//	     try {
//			System.out.println(dataSource.getNumIdleConnections());
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}// 空闲连接数
//	     try {
//			System.out.println(dataSource.getNumConnections());
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}// 总连接数
//	     
	}
	
    
    public DataSource getDataSource() {
		return this.dataSource;
	}
    
	/**

     * 释放连接

     * @param connection

     */
    public static void releaseConnection(Connection connection){

           try {

                  if(connection != null ) {

                         connection.close();

                  }

           }catch (Exception e) {

                  e.printStackTrace();

           }

    }


    /**

     * 获取连接

     * @return

     * @throws SQLException

     */

    public Connection getConnection() throws SQLException{

           return dataSource.getConnection();

    }

	public String getDriver() {
		return Driver;
	}

	public void setDriver(String driver) {
		Driver = driver;
	}

	public String getTable() {
		return table;
	}

	public void setTable(String table) {
		this.table = table;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDataSourceName() {
		return dataSourceName;
	}

	public void setDataSourceName(String dataSourceName) {
		this.dataSourceName = dataSourceName;
	}
    
}
